package com.bss.controller;

import javax.faces.model.DataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.andy.basics.Service;
import com.bss.model.Admission;
import com.bss.service.IdGenerationService;

@Controller
@Scope("request")
public class IdGenerationController extends com.andy.basics.Controller<Admission> {

	@Autowired
	private IdGenerationService idGenerationService;

	private Integer idAdmission = 0;

	public DataModel admissions(final Object patientName) {
		if (patientName == null) {
			return null;
		}
		return this.idGenerationService.getActiveAdmissionData(patientName.toString());
	}

	@Override
	@Transactional
	public String select() {
		Admission admission = (Admission) this.idGenerationService.getData().getRowData();
		this.idGenerationService.loadModelFromId(admission.getId());
		return null;
	}

	public void refresh() {
		if (this.idGenerationService.getModel().getId() != 0) {
			this.idGenerationService.loadModelFromId(this.idGenerationService.getModel().getId());
		}
	}

	public String updateUtensilSet() {
		this.idGenerationService.updateUtensilSet();
		return null;
	}

	@Override
	public Admission create() {
		return new Admission();
	}

	@Override
	public Service<Admission> getService() {
		return this.idGenerationService;
	}

	@Override
	public Admission getModel() {
		if (this.idAdmission == 0) {
			this.idAdmission = this.getAdmissionId();
			if (this.idAdmission != 0) {
				this.idGenerationService.loadModelFromId(this.idAdmission);
			}
		}
		return super.getModel();
	}

	public Integer getAdmissionId() {
		String admissionId = this.getHttpUtil().getRequestParameter("id");
		return admissionId == null ? 0 : Integer.valueOf(admissionId);
	}

	public Integer getUtensilDeposit() {
		return this.idGenerationService.getUtensilDeposit();
	}

	public Integer getSecurityDeposit() {
		return this.idGenerationService.getSecurityDeposit();
	}

}
